import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home'
import Index from '../views/Index'

Vue.use(VueRouter)

const routes = [
    {
        path: '/',
        name: 'Home',
        redirect: './index',
        component: Home,
        children:[
            //首页
            {
                path:'/index',
                name: "Index",
                component: Index
            },
            // 登录页
            {
                path: '/login',
                name: "Login",
                component: () => import('../views/Login.vue')
            },
            // 注册页
            {
                path: '/register',
                name: "Register",
                component: () => import('../views/Register.vue')
            },
            // 购物车
            {
                path: '/cart',
                name: "Cart",
                component: () => import('../views/Cart.vue')
            },
            // 订单结算页
            {
                path: '/order/:id',
                name: "Order",
                component: () => import('../views/Order.vue')
            },
            // 我的账号
            {
                path: '/myaccount',
                name: 'MyAccount',
                redirect: '/myaccount/mymessage',
                component: () => import('../views/MyAccount.vue'),
                beforeEnter:(to, from, next) => {
                    // console.log(Vue.prototype.$Cookie)
                    if(Vue.prototype.$Cookie.get("user")) {
                        next()
                    } else {
                        next('/login?redirect=' + to.fullPath)
                    }
                },
                children:[
                    // 我的账号
                    {
                        path: '/myaccount/mymessage',
                        name: 'MyMessage',
                        component: () => import("../components/MyMessage.vue")
                    },
                    // 我的配送地址
                    {
                        path: '/myaccount/myaddress',
                        name: 'MyAddress',
                        component: () => import("../components/MyAddress.vue")
                    },
                    // 我的订单
                    {
                        path: '/myaccount/myorder',
                        name: 'MyOrder',
                        component: () => import("../components/MyOrder.vue")
                    },
                    // 我的信息详情
                    {
                        path: '/myaccount/mydetails',
                        name: 'MyDetails',
                        component: () => import("../components/MyDetails.vue")
                    },
                    // 我的退货
                    {
                        path: '/myaccount/myreturns',
                        name: 'MyReturns',
                        component: () => import("../components/MyReturns.vue")
                    }
                ]
            },
            // 商品详情页
            {
                path: "/details/:id",
                name: 'Details',
                component: () => import('../views/Details.vue')
            },
            // 退出登录页
            {
                path: "/mylogout",
                name: 'MyLogOut',
                component: () => import('../views/MyLogOut.vue')
            }
        ]
    },  
]

const router = new VueRouter({
    routes,
    scrollBehavior(to, from, savedPosition) {
        if(savedPosition) {
            return savedPosition
        } else {
            return {
                x: 0,
                y: 0
            }
        }
    }
})

export default router
